package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.PerformFreqDict;
import com.zp.base.entity.bo.PerformFreqDictBo;
import com.zp.base.entity.vo.PerformFreqDictVo;
import com.zp.base.mapper.PerformFreqDictMapper;
import com.zp.base.service.IPerformFreqDictService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 执行频次Service业务层处理
 *
 * @author zhang peng
 * @date 2022-12-27
 */
@RequiredArgsConstructor
@Service
public class PerformFreqDictServiceImpl implements IPerformFreqDictService {

    private final PerformFreqDictMapper baseMapper;

    /**
     * 查询执行频次
     */
    @Override
    public PerformFreqDictVo queryById (String freqId) {
        return baseMapper.selectVoById(freqId);
    }

    /**
     * 查询执行频次列表(分页)
     */
    @Override
    public TableDataInfo<PerformFreqDictVo> queryPageList (PerformFreqDictBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PerformFreqDict> lqw = buildQueryWrapper(bo);
        Page<PerformFreqDictVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询执行频次列表(不分页)
     */
    @Override
    public List<PerformFreqDictVo> queryList (PerformFreqDictBo bo) {
        LambdaQueryWrapper<PerformFreqDict> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PerformFreqDict> buildQueryWrapper (PerformFreqDictBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PerformFreqDict> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFreqDesc()), PerformFreqDict::getFreqDesc, bo.getFreqDesc());
        lqw.eq(bo.getFreqCounter() != null, PerformFreqDict::getFreqCounter, bo.getFreqCounter());
        lqw.eq(bo.getFreqInterval() != null, PerformFreqDict::getFreqInterval, bo.getFreqInterval());
        lqw.eq(StringUtils.isNotBlank(bo.getFreqIntervalUnit()), PerformFreqDict::getFreqIntervalUnit, bo.getFreqIntervalUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getDictType()), PerformFreqDict::getDictType, bo.getDictType());
        lqw.orderByAsc(PerformFreqDict::getShowSort);
        return lqw;
    }

    /**
     * 新增执行频次
     */
    @Override
    public Boolean insertByBo (PerformFreqDictBo bo) {
        PerformFreqDict add = BeanUtil.toBean(bo, PerformFreqDict.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFreqId(add.getFreqId());
        }
        return flag;
    }

    /**
     * 修改执行频次
     */
    @Override
    public Boolean updateByBo (PerformFreqDictBo bo) {
        PerformFreqDict update = BeanUtil.toBean(bo, PerformFreqDict.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (PerformFreqDict entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除执行频次
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
